package com.yh.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.Message;
import com.yh.service.MessService;

@Controller
public class MessConteoller {
	
	@Autowired
	@Qualifier("messServiceImpl")
	private MessService service;
	
	/*
	 * 查询通知信息
	 * 前台使用layPage来显示分页信息
	 */
	@RequestMapping("/querymess")
	@ResponseBody
	public Map queryMessage(int curr){
		Map map=new HashMap();
		
		//使用layPage来设置分页格式
		Page page=PageHelper.startPage(curr,4,"mess_id desc");
		List<Message> mlist=service.queryMessage();
		System.out.println("querymess的输出测试："+mlist);
		//total和mlist存贮到map集合中 发送到前台页面
		map.put("total", page.getTotal());	//获取总条数
		map.put("mlist", mlist);
		return map;
	}
	
	/*
	 * 通过id查找通知的具体信息
	 */
	@RequestMapping("/showmess")
	public String showMessage(Model model,int id){
		Message mess=service.showMessage(id);
		model.addAttribute("mess",mess);
		System.out.println("showmess输出测试"+mess);
		return "showMessage";
	}
	
	/*
	 * 删除通告
	 */
	@RequestMapping("/deletemess")
	@ResponseBody
	public int deleteMessage(int meid){
		int a=service.deleteMessage(meid);
		System.out.println(a);
		return a;
	}
	
	/*
	 * 添加通知
	 * 添加当前时间到mess参数中；获取前台的session中userid存入mess对象中传入实现,sql语句中
	 */
	@RequestMapping("/insertmess")
	@ResponseBody
	public int insertMessage(Message mess,HttpSession session){
		//获取session对象中userid
		int mess_userid=(int) session.getAttribute("mess_userid");
		//获取当前时间并添加到mess参数中
		Date mess_time=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
		//time,id发送到mess对象中
		mess.setMess_userid(mess_userid);
		mess.setMess_time(sdf.format(mess_time));
		System.out.println("insert时间测试："+mess.getMess_time());
		//接受前台session存入session对象中
		
		int a=service.insertMessage(mess);
		System.out.println("添加contro层"+a);
		return a;
	}
}
